<?php
/*
    @登录模型
    @author 高天杰  2014-6-14
*/
class LoginModel extends Model 
{
    /**
     * 登录的验证
     * @string$ username 用户名 .
     * @string $userpass 密码 (md5加密).
     * @return bool.
     */
    public static function loginAuth($username, $userpass)
    {
        if (empty($username) || empty($userpass))
        {
            return false;
        }

        $where = array("u_name" => $username, "status" => "1");
        $login = M("user1")->field("`u_psw`, `status`,`u_name`,`group_id`,`u_type`,`u_image`,`u_sex`")->where($where)->limit(1)->find();

        if ($login['u_psw'] == md5($userpass))
        {
            if (self::setSession($login))
            {
                //如果登录的使用单独SESSION.判断是否登录.
                self::setSession(array(SESSION_ISLOGIN => "1"));
                UserLoginModel::recordLoginLog();
                return true;
            }
        }

        return false;
    }
    /**
     * 设置session.
     * array $session.需要设置SESSION的数组
     * @ return bool.
     */
    public static function setSession($session)
    {
        if (empty($session) || !is_array($session))
        {
            return false;
        }
        else
        {
            foreach ($session as $key => $value) 
            {
                session($key, $value);
            }

            return true;
        }

        return false;
    }

    //销毁session.
    public static function destroySession()
    {
        session(null);
    }

    /**
     * 找回密码
     *
     * @param   string   $username   用户名
     * @param   string   $email      邮箱
     */
    public static function forget($username, $email)
    {
        if (empty($username) || empty($email)) {
            return false;
        }
    }
}

?>